package com.xinheyun.demo.leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.Data;

/**
 * @description: TODO
 * @author: Sarah
 * @modified By: Sarah
 * @date: Created in 2021/8/31 16:32
 * @version:v1.0
 */

public class test355 {


    public static void main(String[] args) {
        Twitter twitter = new Twitter();

    }


}

@Data
class Twitter {

    List<Atweet> tweet;
    Map<Integer,Set<Integer>> follow;

    public static class Atweet{
        int userId;
        int tweetId;

        public Atweet(int userId, int tweetId) {
            this.userId = userId;
            this.tweetId = tweetId;
        }
    }

    /** Initialize your data structure here. */
    public Twitter() {
        tweet = new ArrayList<>();
        follow = new HashMap<>();
    }

    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
        if(!follow.containsKey(userId)){
            HashSet<Integer> objects = new HashSet<>();
            objects.add(userId);
            follow.put(userId,objects);
        }
        tweet.add(new Atweet(userId,tweetId));
    }

    /** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */
    public List<Integer> getNewsFeed(int userId) {
        List<Integer> list = new ArrayList<>();
        int i =0;
        Set<Integer> set = follow.get(userId);
        for(int j = tweet.size()-1;j>=0;j--){
            if(set.contains(tweet.get(j).userId)){
                list.add(tweet.get(j).tweetId);
                i++;
                if(i>=10){
                    break;
                }
            }
        }
        return list;

    }

    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    public void follow(int followerId, int followeeId) {
        if(!follow.containsKey(followerId)){
            return;
        }
        follow.get(followerId).add(followeeId);
    }

    /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
    public void unfollow(int followerId, int followeeId) {
        if(!follow.containsKey(followerId)){
            return;
        }
        follow.get(followerId).remove(followeeId);
    }
}

